#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime

from sqlalchemy import Column, Integer, Date, Float, and_, Text
from sqlalchemy.orm import declarative_base
from datetime import date

import init
from data_alchemy.account_voucher_alchemy import AccountVoucherAlchemy
from init import session
from initLogger import logger
from toolkit import fromVoucherPathToName

Base = declarative_base()




class InvestmentFlowAlchemy(Base):
    '''
    类功能：用来记录投资流数据
    '''

    __tablename__ = "investment_flow"

    id = Column(Integer, primary_key=True, autoincrement=True, comment="")
    investment_date = Column(Date, comment="资金流动时间")
    year = Column(Integer, comment="资金流动年份")
    month = Column(Integer, comment="资金流动月份")
    investment_value = Column(Float, comment="资金金额")
    investment_path = Column(Text, comment="投资途径")
    investment_name = Column(Text, comment="投资名称")  #为后期投资途径相同的不同投资，用这个字段做区分。
    flow_type = Column(Text, comment="投资流程的类型")   #包括：定投、分红、取现等。
    voucher_id = Column(Integer, comment="对应的记账凭证ID")

    def __init__(self,
                 investmentDate = None,
                 investmentValue = 0.0,
                 investmentPath ="",
                 flowType = "",
                 voucherId= 0
                 ):
        self.investment_date = investmentDate
        if investmentDate!=None:
            self.year = self.investment_date.year
            self.month = self.investment_date.month
        self.investment_value = investmentValue
        self.investment_path = investmentPath
        self.investment_name =  fromVoucherPathToName(self.investment_path)
        self.flow_type = flowType
        self.voucher_id = voucherId

    def AddInvestmentItem(self):
        '''
        函数功能；添加一条投资流水记录
        :return:
        '''
        session.add(self)
        session.commit()


    def SearchInvestmentFlowByPath(self, investmentPath):
        '''
        函数功能：查询指定投资品种的投资流水记录。
        :param investmentPath:
        :param flowType:
        :return:
        '''

        investmentFlowInfo = session.query(InvestmentFlowAlchemy).filter(InvestmentFlowAlchemy.investment_path == investmentPath) \
                                                                 .order_by(InvestmentFlowAlchemy.investment_date)  \
                                                                 .all()

        return  investmentFlowInfo

    def SearchInvestmentFlowByVoucherId(self, voucherId):
        '''
        函数功能：查找与指定记账凭证id相关联的投资数据流记录
        :param voucherId:
        :return:
        '''

        investmentFlowInfo = session.query(InvestmentFlowAlchemy).filter(InvestmentFlowAlchemy.voucher_id == voucherId) \
                                                                 .order_by(InvestmentFlowAlchemy.investment_date)  \
                                                                 .all()

        return  investmentFlowInfo

    def SearchInvestmentFlowByMonth(self, year, month):
        '''
        函数功能：查找指定月份的投资数据流记录
        :param year:
        :param month:
        :return:
        '''
        investmentFlowInfo = session.query(InvestmentFlowAlchemy).filter(and_(InvestmentFlowAlchemy.year==year, InvestmentFlowAlchemy.month==month)) \
                                                                 .order_by(InvestmentFlowAlchemy.investment_date)  \
                                                                 .all()

        return  investmentFlowInfo

    def SearchInvestmentFlowByName(self, investmentName):
        '''
        函数功能：通过投资品名称查询投资流水记录
        :param investmentName:
        :return:
        '''

        investmentFlowInfo = session.query(InvestmentFlowAlchemy).filter(InvestmentFlowAlchemy.investment_name == investmentName) \
                                                                 .order_by(InvestmentFlowAlchemy.investment_date)  \
                                                                 .all()

        return  investmentFlowInfo

    def DelInvestmentFlowItem(self, id):
        '''
        函数功能：删除一条投资数据流记录
        :param id:
        :return: 成功返回True，失败返回False
        '''
        deleteInvestmentFlowItem = session.query(InvestmentFlowAlchemy).filter(InvestmentFlowAlchemy.id == id).first()
        if deleteInvestmentFlowItem:
            session.delete(deleteInvestmentFlowItem)
            session.commit()
            return True
        else:
            return False

    def  ModifiyInvestmentFlowItem(self, id):
        '''
        函数功能：将指定id在数据库中的内容，修改当前投资数据流记录
        :param id:
        :return:
        '''
        # 有需求时再具体实现

        investmentFlowAlchemyItem = session.query(InvestmentFlowAlchemy).filter_by(id=id).first()
        investmentFlowAlchemyItem.investment_date = self.investment_date

        # 修改的bug，调试一下
        investmentFlowAlchemyItem.year = self.year
        investmentFlowAlchemyItem.month = self.month
        investmentFlowAlchemyItem.investment_value = self.investment_value
        investmentFlowAlchemyItem.investment_path = self.investment_path
        investmentFlowAlchemyItem.investment_name = self.investment_name
        investmentFlowAlchemyItem.flow_type = self.flow_type
        investmentFlowAlchemyItem.voucher_id = self.voucher_id

        session.commit()

    def GetInvestmentPathList(self):
        '''
        函数功能：获取投资路径信息列表
        :return:
        '''

        #investmentPath="资产->投资->理财->招商产业债券A基金"
        investmentPathList = session.query(InvestmentFlowAlchemy.investment_path).distinct().all()

        return investmentPathList

def main():

    '''
    investmentFlowAlchemy = InvestmentFlowAlchemy(investmentDate=date(2023, 7, 28),
                                                  investmentValue= -1000.00,
                                                  investmentPath="资产->投资->理财->招商产业债券A基金",
                                                  flowType="定投",
                                                  voucherId=10003 )
    investmentFlowAlchemy.AddInvestmentItem()
    '''

    investmentFlowAlchemy = InvestmentFlowAlchemy()
    investmentPathList = investmentFlowAlchemy.GetInvestmentPathList()

    logger.debug(investmentPathList)





InvestmentFlowAlchemy.metadata.create_all(init.engine)
if __name__ == '__main__':
    main()